package org.lex.filter.swing;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.concurrent.TimeUnit;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.lex.filter.Filter;
import org.lex.filter.Filters;
import org.lex.filter.RangeFilter;
import org.lex.swing.Swings;
import org.lex.swing.widgets.JLabelSeparator;


public class DateFilterPane extends AbstractFilterPane<Long> {
	private static final long DAY = TimeUnit.DAYS.toMillis(1);
	private static final long WEEK = 7 * DAY;
	private static final long MONTH = 30 * DAY;
	private static final long YEAR = 365 * DAY;

	private JLabelSeparator tipLabel = null;
	private JRadioButton notRememberRadioButton = null;
	private JRadioButton lastWeekRadioButton = null;
	private JRadioButton lastMonthRadioButton = null;
	private JRadioButton lastYearRadioButton = null;
	private JRadioButton specifyRadioButton = null;
	private JPanel specifyPane = null;
	private JLabel fromLabel = null;
	private JTextField fromTextField = null;
	private JLabel toLabel = null;
	private JTextField toTextField = null;
	private JPanel gluePane = null;

	public DateFilterPane() {
		super();
		initialize();
	}

	public String getTitle() {
		return "Date";
	}

	/*
	 * ����ġ���ȥ��һ���¡�������ȥ��һ�ꡱӦ�ø�ݵ�ǰ�������жϵ����Ƕ�����
	 * ����Ϊ�˼����⣬�򵥵���Ϊ����=30�죬��=365��
	 */
	public Filter<Long> getFilter() {
		Filter<Long> filter = null;
		long now = System.currentTimeMillis();
		if (true == this.notRememberRadioButton.isSelected())
			filter = Filters.acceptAllFilter();
		else if (true == this.lastWeekRadioButton.isSelected())
			filter = new RangeFilter<Long>(now - WEEK, RangeFilter.Mask.GE);
		else if (true == this.lastMonthRadioButton.isSelected())
			filter = new RangeFilter<Long>(now - MONTH, RangeFilter.Mask.GE);
		else if (true == this.lastYearRadioButton.isSelected())
			filter = new RangeFilter<Long>(now - YEAR, RangeFilter.Mask.GE);
		else {
			// specify date, not implements yet
			filter = Filters.acceptAllFilter();
		}
		return filter;
	}

	private void initialize() {
		GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
		gridBagConstraints21.gridx = 0;
		gridBagConstraints21.gridwidth = 2;
		gridBagConstraints21.fill = GridBagConstraints.BOTH;
		gridBagConstraints21.weightx = 1.0;
		gridBagConstraints21.weighty = 1.0;
		gridBagConstraints21.gridy = 5;
		GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
		gridBagConstraints11.gridx = 0;
		gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints11.gridwidth = 2;
		gridBagConstraints11.weightx = 1.0;
		gridBagConstraints11.gridy = 4;
		GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
		gridBagConstraints5.gridx = 0;
		gridBagConstraints5.anchor = GridBagConstraints.WEST;
		gridBagConstraints5.insets = new Insets(0, 6, 0, 0);
		gridBagConstraints5.gridy = 3;
		GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
		gridBagConstraints4.gridx = 1;
		gridBagConstraints4.anchor = GridBagConstraints.WEST;
		gridBagConstraints4.gridy = 2;
		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		gridBagConstraints3.gridx = 0;
		gridBagConstraints3.anchor = GridBagConstraints.WEST;
		gridBagConstraints3.insets = new Insets(0, 6, 0, 0);
		gridBagConstraints3.gridy = 2;
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.gridx = 1;
		gridBagConstraints2.anchor = GridBagConstraints.WEST;
		gridBagConstraints2.gridy = 1;
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.gridx = 0;
		gridBagConstraints1.anchor = GridBagConstraints.WEST;
		gridBagConstraints1.insets = new Insets(0, 6, 0, 0);
		gridBagConstraints1.gridy = 1;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = GridBagConstraints.WEST;
		gridBagConstraints.insets = new Insets(5, 12, 0, 5);
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridy = 0;
		tipLabel = new JLabelSeparator("When was it modified?");
		this.setSize(300, 200);
		this.setLayout(new GridBagLayout());
		this.add(tipLabel, gridBagConstraints);
		this.add(getNotRememberRadioButton(), gridBagConstraints1);
		this.add(getLastWeekRadioButton(), gridBagConstraints2);
		this.add(getLastMonthRadioButton(), gridBagConstraints3);
		this.add(getLastYearRadioButton(), gridBagConstraints4);
		this.add(getSpecifyRadioButton(), gridBagConstraints5);
		this.add(getSpecifyPane(), gridBagConstraints11);
		this.add(getGluePane(), gridBagConstraints21);
		Swings.createButtonGroup(notRememberRadioButton, lastWeekRadioButton, lastMonthRadioButton, lastYearRadioButton, specifyRadioButton);
	}

	private JRadioButton getNotRememberRadioButton() {
		if (notRememberRadioButton == null) {
			notRememberRadioButton = new JRadioButton();
			notRememberRadioButton.setSelected(true);
			notRememberRadioButton.setText("Don't remember");
		}
		return notRememberRadioButton;
	}

	private JRadioButton getLastWeekRadioButton() {
		if (lastWeekRadioButton == null) {
			lastWeekRadioButton = new JRadioButton();
			lastWeekRadioButton.setText("Within the past week");
		}
		return lastWeekRadioButton;
	}

	private JRadioButton getLastMonthRadioButton() {
		if (lastMonthRadioButton == null) {
			lastMonthRadioButton = new JRadioButton();
			lastMonthRadioButton.setText("Last month");
		}
		return lastMonthRadioButton;
	}

	private JRadioButton getLastYearRadioButton() {
		if (lastYearRadioButton == null) {
			lastYearRadioButton = new JRadioButton();
			lastYearRadioButton.setText("Within the past year");
		}
		return lastYearRadioButton;
	}

	private JRadioButton getSpecifyRadioButton() {
		if (specifyRadioButton == null) {
			specifyRadioButton = new JRadioButton();
			specifyRadioButton.setText("Specify dates");
		}
		return specifyRadioButton;
	}

	private JPanel getSpecifyPane() {
		if (specifyPane == null) {
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.insets = new Insets(0, 28, 0, 0);
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints8.gridy = 1;
			gridBagConstraints8.weightx = 1.0;
			gridBagConstraints8.insets = new Insets(0, 5, 0, 5);
			gridBagConstraints8.gridx = 1;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.insets = new Insets(0, 28, 0, 0);
			gridBagConstraints7.gridy = 1;
			toLabel = new JLabel();
			toLabel.setText("to");
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.gridy = 0;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.insets = new Insets(0, 5, 5, 5);
			gridBagConstraints6.gridx = 1;
			fromLabel = new JLabel();
			fromLabel.setText("from");
			specifyPane = new JPanel();
			specifyPane.setLayout(new GridBagLayout());
			specifyPane.add(fromLabel, gridBagConstraints9);
			specifyPane.add(getFromTextField(), gridBagConstraints6);
			specifyPane.add(toLabel, gridBagConstraints7);
			specifyPane.add(getToTextField(), gridBagConstraints8);
		}
		return specifyPane;
	}

	private JTextField getFromTextField() {
		if (fromTextField == null) {
			fromTextField = new JTextField();
		}
		return fromTextField;
	}

	private JTextField getToTextField() {
		if (toTextField == null) {
			toTextField = new JTextField();
		}
		return toTextField;
	}

	private JPanel getGluePane() {
		if (gluePane == null) {
			gluePane = new JPanel();
			gluePane.setLayout(new GridBagLayout());
		}
		return gluePane;
	}
}
